package personal.zq.practice.common.utils;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.ws.security.util.Base64;

public class Des3Util {
    // 密钥 长度不得小于24
    private final static String SECRETKEY = "planbookPro1233211234567" ;
    // 向量 可有可无 终端后台也要约定
    private final static String IV = "Cpic1234";
    // 加解密统一使用的编码方式
    private final static String ENCODING = "utf-8";

    /**
    * 3DES加密
    *
    * @param plainText
    *            普通文本
    * @return
    * @throws Exception
    */
    public static String encode(String plainText) throws Exception {
         Key deskey = null;
         DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY .getBytes());
         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
          deskey = keyfactory.generateSecret( spec);

         Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding");
         IvParameterSpec ips = new IvParameterSpec( IV.getBytes());
          cipher.init(Cipher. ENCRYPT_MODE, deskey, ips);
          byte[] encryptData = cipher.doFinal( plainText.getBytes( ENCODING));
          return Base64.encode( encryptData);
   }
//
//    /**
//    * 3DES解密
//    *
//    * @param encryptText
//    *            加密文本
//    * @return
//    * @throws Exception
//    */
//    public static String decode(String encryptText) throws Exception {
//         Key deskey = null;
//         DESedeKeySpec spec = new DESedeKeySpec( SECRETKEY.getBytes());
//         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
//          deskey = keyfactory. generateSecret( spec);
//         Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding" );
//         IvParameterSpec ips = new IvParameterSpec( IV.getBytes());
//          cipher. init(Cipher. DECRYPT_MODE, deskey, ips);
//
//          byte[] decryptData = cipher. doFinal(Base64. decode(encryptText ));
//
//          return new String( decryptData, ENCODING);
//   }
//
///*    public static void main(String args[]) throws Exception{
//         String str = "BEJD1081" ;
//         System. out.println( "----加密前-----：" + str );
//         String encodeStr = Des3Util.encode(str);
//         System. out.println( "----加密后-----：" + encodeStr );
//         System. out.println( "----解密后-----：" + Des3Util.decode(encodeStr));
//   }*/
//
// // 解密数据
// 	public static String decrypt(String message, String key) throws Exception {
// 		byte[] bytesrc = convertHexString(message);
// 		Cipher cipher = Cipher.getInstance("des/CBC/PKCS5Padding");
// 		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
// 		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
// 		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
// 		IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
//
// 		cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
//
// 		byte[] retByte = cipher.doFinal(bytesrc);
//
// 		return new String(retByte);
// 	}
//
// 	public static byte[] encrypt2Byte(String message, String key)
// 			throws Exception {
// 		Cipher cipher = Cipher.getInstance("des/CBC/PKCS5Padding");
//
// 		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
//
// 		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
// 		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
// 		IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
// 		cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
//
// 		return cipher.doFinal(message.getBytes("UTF-8"));
// 	}
//
// 	public static byte[] convertHexString(String ss) {
// 		byte[] digest = new byte[ss.length() / 2];
//
// 		for (int i = 0; i < digest.length; i++) {
// 			String byteString = ss.substring(2 * i, (2 * i) + 2);
// 			int byteValue = Integer.parseInt(byteString, 16);
// 			digest[i] = (byte) byteValue;
// 		}
//
// 		return digest;
// 	}
//
// 	public static String encrypt(String message, String key) throws Exception {
// 		return toHexString(encrypt2Byte(message, key)).toUpperCase();
// 	}
//
// 	public static String toHexString(byte[] b) {
// 		StringBuffer hexString = new StringBuffer();
//
// 		for (int i = 0; i < b.length; i++) {
// 			String plainText = Integer.toHexString(0xff & b[i]);
//
// 			if (plainText.length() < 2) {
// 				plainText = "0" + plainText;
// 			}
//
// 			hexString.append(plainText);
// 		}
//
// 		return hexString.toString();
// 	}
}
